package com.javafactroy.kunlong;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONUtil;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import com.javafactroy.kunlong.framework.elasticsearch.mapper.DptMapper;
import com.javafactroy.kunlong.framework.elasticsearch.model.EsichubSysDept;
import com.jtest.annotation.JTestInject;
import com.jtest.framework.ITestFixture;
import com.jtest.nodeFactroy.Inject.Inject;
import com.jtest.nodeFactroy.Node.HttpClientNode;
import com.jtest.nodeFactroy.Node.JDbNode;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
@Slf4j
public class TestSpringWithJtest extends ITestFixture {

    @JTestInject("hrms")
    JDbNode hrms;
    @Inject(filename = "node.xml", value = "test")
    HttpClientNode test;

    @Test
    public void test001_dbcheck() throws Exception {

        List<Map<String, Object>> list = hrms.clearSql().sql("select * from department limit 2").select();
        Console.log(JSONUtil.toJsonPrettyStr(list));
        hrms.checkRecordNumber(2);
        hrms.checkRecord("id=1");
    }

    @Test
    public void test002_webget() throws Exception {
        String tt = test.get("http://www.baidu.com");
        Console.log(tt);

    }

    @Autowired
    DptMapper dptMapper;

    @Test
    public void test003_ee() {
        // dptMapper.deleteIndex("test_jd");
        //  dptMapper.createIndex("test_jd");
        EsichubSysDept es = new EsichubSysDept();
        es.setId(3l);
        es.setDeptName("4");
        es.setLeader("44");
        dptMapper.insertBatch(Arrays.asList(es));

        LambdaEsQueryWrapper<EsichubSysDept> query = new LambdaEsQueryWrapper<>();

        //相当于 select * from test_jc where name like '%红%' and sex = 29 and industryTags in ('标签1','标签2')
        //  query.and(item -> item.match(EsichubSysDept::getDeptName, "1"));

        //query.in("industryTags", Arrays.asList("标签1", "标签2"));
        // 默认按查询度倒叙
        //Func<Object, Object> lambdaEsQueryWrapper;
        query.sortByScore(SortOrder.DESC);
        //注意：从1开始起步 不是从0开始
        EsPageInfo<EsichubSysDept> pageQuery = dptMapper.pageQuery(query, 1, 10);
        //查询数据
        System.out.println(CommonUtils.toJSONStringPretty(pageQuery.getList()));


    }

}
